package net.qhzw.vip.qiyu.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.entity.DtsUser;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.model.*;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.result.PageResult;
import net.qhzw.vip.qiyu.service.DtsUserService;
import net.qhzw.vip.qiyu.service.SysLoginService;
import net.qhzw.vip.qiyu.service.WxAuthService;
import net.qhzw.vip.qiyu.util.PageAdapter;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Api(tags = "用户管理")
@RestController
@Slf4j
public class MemberController {


    @Autowired
    private WxAuthService wxAuthService;

    @Autowired
    private DtsUserService dtsUserService;

    @Autowired
    private SysLoginService loginService;

    @GetMapping("getUserOpenidById/{id}")
    public JsonResult<String> getUserOpenidById(@PathVariable Long id) {
        DtsUser dtsUser = dtsUserService.getById(id);
        if (dtsUser == null) {
            throw new ApiException("用户id错误！");
        }
        return JsonResult.ok(dtsUser.getWeixinOpenid());
    }


    @ApiOperation(value = "分页查询用户")
    @GetMapping("members")
    public JsonResult<Page<DtsUser>> findByPage(Page<DtsUser> page, DtsUser dtsUser) {
        Page<DtsUser> userPage = dtsUserService.findByPage(page, dtsUser);
        return JsonResult.ok(userPage);
    }

    @ApiOperation(value = "分页查询实名认证用户")
    @GetMapping("sys/members/authStatus")
    public JsonResult<PageResult<DtsUser>> findByPage(Page<DtsUser> page, String realName, String idCard, String mobile, Byte authStatus) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        PageResult<DtsUser> dtsUserPage = dtsUserService.findAauthPage(new PageAdapter(page), realName, idCard, mobile, authStatus, shopId);
        return JsonResult.ok(dtsUserPage);
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("members")
    public JsonResult addDtsUser(@RequestBody DtsUser dtsUser) {
        boolean save = dtsUserService.addDtsUser(dtsUser);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "根据id查询实名认证用户")
    @GetMapping("sys/members/authStatus/{id}")
    public JsonResult<DtsUser> getDtsUser(@PathVariable Long id) {
        DtsUser dtsUser = dtsUserService.getById(id);
        return JsonResult.ok(dtsUser);
    }


    @ApiOperation(value = "审核实名认证")
    @PutMapping("sys/members/verified")
    public JsonResult verified(@RequestBody VerifiedForm verifiedForm) {
        boolean update = dtsUserService.verified(verifiedForm);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改用户")
    @PutMapping("members")
    public JsonResult updateDtsUser(@RequestBody DtsUser dtsUser) {
        boolean update = dtsUserService.updateDtsUser(dtsUser);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改用户信息")
    @PutMapping("member/modifyUsrInfo")
    public JsonResult modifyUsrInfo(@RequestBody ModifyUsrInfoParam param) {
        boolean update = dtsUserService.modifyUsrInfo(param);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置用户状态")
    @PutMapping("members/setStatus")
    public JsonResult setStatus(@RequestBody Map<String, String> map) {
        Long id = Long.valueOf(map.get("id"));
        Byte status = Byte.valueOf(map.get("status"));
        DtsUser dtsUser = new DtsUser();
        dtsUser.setId(id);
        dtsUser.setStatus(status);
        boolean save = dtsUserService.updateDtsUser(dtsUser);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除用户")
    @DeleteMapping("members/{id}")
    public JsonResult removeDtsUser(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = dtsUserService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


  /*  @ApiOperation(value = "微信授权登录")
    @PostMapping("members/loginByWeixin")
    public JsonResult loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo,
                                    HttpServletRequest request) {
        LoginUser loginUser = wxAuthService.loginByWeixin(wxLoginInfo, request);
        return JsonResult.ok(loginUser);
    }*/


    @ApiOperation(value = "站点管理员登录")
    @PostMapping("members/login")
    public JsonResult<LoginUser> login(@RequestBody @Validated LoginForm param) {
        param.setLoginType("member_type");
        LoginUser loginUser = loginService.login(param);
        return JsonResult.ok(loginUser);
    }

    @ApiOperation(value = "用户认证")
    @PostMapping("members/authAccount")
    public JsonResult<LoginUser> authAccount(@RequestBody UserAuthForm userAuthForm) {
        log.info("【用户认证】：{}", JSON.toJSONString(userAuthForm));
        Long userId = SecurityUtils.getUserId();
        boolean isOk = dtsUserService.identifyVerify(userId, userAuthForm);
        if (isOk) {
            return JsonResult.ok();
        }
        return JsonResult.fail("认证失败");
    }

    @ApiOperation(value = "获取登录用户信息")
    @GetMapping("members/loginInfo")
    public JsonResult<DtsUser> loginInfo() {
        Long userId = SecurityUtils.getUserId();
        DtsUser dtsUser = dtsUserService.getById(userId);
        return JsonResult.ok(dtsUser);
    }

    @ApiOperation(value = "获取登录用户信息")
    @GetMapping("members/loginInfoById/{userId}")
    public JsonResult<DtsUser> loginInfoById(@PathVariable Long userId) {
        DtsUser dtsUser = dtsUserService.getById(userId);
        return JsonResult.ok(dtsUser);
    }

    /**
     * 退出登录,并清除redis中的token
     **/
    @GetMapping("members/logout")
    public JsonResult removeToken() {
        return JsonResult.ok();
    }


}
